﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class Manage_ListArticles : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            TreasurerDataContext db = new TreasurerDataContext();
            var q = from aF in db.Forums
                    select new { ID = aF.ID, Name = aF.Name, Parent = aF.ParentID };

            ListItem li = new ListItem("全部栏目", "-");
            ForumList.Items.Add(li);
            foreach (var o in q)
            {
                li = new ListItem(o.Name, o.ID);
                ForumList.Items.Add(li);
            }

            string fid = Request.Params["fid"];
            if (fid != null && fid != "")
            {
                ForumList.SelectedValue = fid;
            }
        }

        if (ForumList.SelectedValue == "-")
        {
            ArticleDataSource.Where = "";
            ArticleView.DataBind();
        }
        else
        {
            ArticleDataSource.Where = "ForumID == @ForumID";
            ArticleDataSource.WhereParameters.Clear();
            ArticleDataSource.WhereParameters.Add(new Parameter("ForumID", System.Data.DbType.String, ForumList.SelectedValue));
            ArticleView.DataBind();
        }
    }
    protected void NewArticleButton_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Manage/EditArticle.aspx");
    }
    protected void ArticleView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int idx = e.NewEditIndex;
        string id = ArticleView.Rows[idx].Cells[1].Text;
        Response.Redirect("~/Manage/EditArticle.aspx?aid=" + id);
    }

    protected void Command_DeleteArticle(Object sender, CommandEventArgs e)
    {
        if (e.CommandArgument != null)
        {
            string id = (string)e.CommandArgument;
            TreasurerDataContext db = new TreasurerDataContext();
            var q = from aA in db.Articles
                    where aA.ID == id
                    select aA;
            db.Articles.DeleteAllOnSubmit(q);
            db.SubmitChanges();

            Response.Write("<script>alert('所选文章信息已经被成功删除。');document.location='ListArticles.aspx';</script>");
        }
    }

    protected void Command_ChangeForum(Object sender, CommandEventArgs e)
    {
        if (e.CommandArgument != null)
        {
            string fid = (string)e.CommandArgument;
            ForumList.SelectedValue = fid;
            ArticleDataSource.Where = "ForumID == @ForumID";
            ArticleDataSource.WhereParameters.Clear();
            ArticleDataSource.WhereParameters.Add(new Parameter("ForumID", System.Data.DbType.String, ForumList.SelectedValue));
            ArticleView.DataBind();
        }
    }
    protected void RegenerateArticles_Click(object sender, EventArgs e)
    {
        TreasurerDataContext db = new TreasurerDataContext();
        var q = from aA in db.Articles
                select aA;
        foreach (Articles a in q)
        {
            a.Template = "文章样式1";

            #region 生成静态页面
            string dst = Server.MapPath(string.Format(@"~/Statics/{0}.html", a.ID));

            //如果目标文件存在，则删除
            if (File.Exists(dst))
            {
                File.Delete(dst);
            }

            //获取原文件输入流
            TextWriter tw = new StringWriter();
            Server.Execute(string.Format(@"~/Templates/{0}.aspx?aid={1}", a.Template, a.ID), tw);

            StreamWriter writer = new StreamWriter(dst, false, System.Text.Encoding.UTF8);
            writer.Write(tw.ToString());

            writer.Close();
            tw.Close();
            #endregion
        }

        db.SubmitChanges();
    }
}